<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Database.KumomadoDatabase');
	/**
	 * @package libraries/database/
	 * @author Tsai, Min Che
	 * @version 1.0
	 * @abstract
	 */
	abstract class KumomadoAbstractDatabase implements KumomadoDatabase {
		/**
		 * @var resource
		 */
		protected $connect;
		/**
		 * @var string
		 */
		private $prefix;
		/**
		 * Construct
		 * @param string $db_type[optional]
		 * @param string $config_name[optional]
		 * @param string $prefix[optional]
		 */
		public function __construct($db_name = 'MySQL', $config_name = 'MySQL', $prefix = 'kumomado') {}
		/**
		 * @abstract
		 */
		abstract public function connect();
		/**
		 * @abstract
		 * @param string $sql
		 * @param int $limit
		 * @param int $start
		 */
		abstract public function query($sql, $limit = 0, $start = 0);
		/**
		 * @abstract
		 */
		abstract public function getInsertId();
		/**
		 * @abstract
		 * @param resource $result
		 */
		abstract public function fetchRow($result);
		/**
		 * @abstract
		 * @param resource $result
		 */
		abstract public function fetchArray($result);
		/**
		 * @abstract
		 * @param resource $result
		 */
		abstract public function getRowsNum($result);
		/**
		 * @abstract
		 */
		abstract public function getAffectedRows();
		/**
		 * @abstract
		 */
		abstract public function close();
		/**
		 * Get the database's configuration
		 * @return array $db_config Database's configuration
		 * @param string $db_type Database type
		 * @param string $config_name config's filename
		 */
		protected function _getDatabaseConfig($db_name = 'MySQL', $config_name = 'MySQL') {
			$config_file = KUMOMADO_LIBRARIES_PATH.'database'.DIRECTORY_SEPARATOR.strtolower($db_name).DIRECTORY_SEPARATOR.$config_name.'.config.php';
			if (file_exists($config_file)) {
				$db_config = require_once $config_file;
			} else {
				throw new KumomadoDatabaseException('The database config cloud not be loaded.');
			}
			if (!is_array($db_config)) {
				throw new KumomadoDatabaseException('The database config not defined.');
			}
			return $db_config;
		}
		/**
		 * Set table's prefix
		 * @param string $prefix table's prefix
		 */
		protected function _setPrefix($prefix = '') {
			$this->prefix = $prefix;
		}
		/**
		 * Add prefix before table name
		 * @return string $prefix
		 * @param string $tableName
		 */
		public function prefix($table_name) {
			if ($table_name != '') {
				return $this->prefix.'_'.$table_name;
			} else {
				return $this->prefix;
			}
		}
	}
?>